package com.project.frame;

import com.project.role.*;
import com.project.util.InitImage;
import com.project.util.KeyPress;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;


public class GameStartFrame extends JFrame implements Runnable {

    /**
     * 游戏是否运行
     */
    public static boolean isRun = true;

    /**
     * 内存画笔
     */
    private Graphics memoryGraphics;
    /**
     * 内存画布
     */
    private Image memoryImage;
    /**
     * 女孩
     */
    public static Girl girl = new Girl();
    /**
     * BOSS
     */
    public static Boss boss = new Boss();
    /**
     * 机器狗
     */
    public static DogMachine dogMachine = new DogMachine();

    /**
     * 地图
     */
    private GameMap gameMap = new GameMap();
    /**
     * 角色集合
     */
    public static List<BaseRole> roleList = new ArrayList<>();


    public GameStartFrame() {
        this.setSize(1000, 700);
        //不能改变窗体大小
        this.setResizable(false);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);




        //图片初始化
        InitImage.init(this);

        roleList.add(girl);
        roleList.add(boss);
        roleList.add(dogMachine);



        //创建缓冲区内存画布
        this.memoryImage = this.createImage(1000, 700);
        //从内存画布中得到内存画笔，内存画笔绘制的内容在内存画布
        this.memoryGraphics = this.memoryImage.getGraphics();


        //添加键盘监听事件
        this.addKeyListener(new KeyPress());
        new Thread(this).start();
    }

    @Override
    public void run() {
        while (isRun) {

            this.drawFrame(this.getGraphics());
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void drawFrame(Graphics g) {

        gameMap.drawMySelf(this.memoryGraphics);

        for (int i = 0; i < roleList.size(); i++) {
            roleList.get(i).drawMyself(this.memoryGraphics);
        }


        //判断碰撞区域是否相交
//        System.out.println(girlRect.intersects(bossRect));
        //将内存中叠加后的图片，绘制到窗体
        g.drawImage(this.memoryImage, 0, 0, this);
    }

    public static void main(String[] args) {
        new GameStartFrame();
    }


}


